const app = new getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    prompt: '',
    default_img: {},
    shop_config: {},
    instance_id: 0,
    promoter_info: {
      team_count: 0,
      commission: {
        commission_cash: '0.00',
        commission: '0.00'
      }
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadShopConfig();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.defaultImg(this, true);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },
  
  /**
   * 加载会员基础信息
   */
  loadInfo: function () {
    let that = this;

    app.sendRequest({
      url: 'System.Member.memberInfo',
      success: function (res) {
        
        if (res.code == 0) {
          //头像图片处理
          let member_info = app.checkEmpty(res.data, {});
          member_info.user_info = app.checkEmpty(member_info.user_info, {});
          member_info.user_info.user_headimg = app.checkEmpty(member_info.user_info.user_headimg, '');
          if (member_info.user_info.user_headimg == '') {
            let default_img = that.data.default_img;

            if (default_img.is_use == 1) {
              member_info.user_info.user_headimg = default_img.value.default_headimg;
            }
          } else {
            member_info.user_info.user_headimg = app.IMG(member_info.user_info.user_headimg);
          }
          let member_img = member_info.user_info.user_headimg;

          that.setData({
            member_info: member_info,
            member_img: member_img,
          })
          that.loadPromoterInfo();
        }
      }
    })
  },

  /**
   * 推广员信息
   */
  loadPromoterInfo: function () {
    let that = this;

    app.sendRequest({
      url: 'System.Distribution.promoterDetail',
      success: function (res) {
        if (res.code == 0) {
          let promoter_info = app.checkEmpty(res.data, '');

          that.setData({
            promoter_info: promoter_info
          })
          if (promoter_info != '') {
            that.loadDistributionGoodsList();
          }
        }
      }
    })
  },
  
  /**
   * 店铺配置
   */
  loadShopConfig: function () {
    let that = this;

    app.sendRequest({
      url: 'System.Distribution.shopConfig',
      success: function (res) {
        if (res.code == 0) {
          let shop_config = app.checkEmpty(res.data, {});

          that.setData({
            shop_config: shop_config
          })
        }
      }
    });
  },

  /**
   * 分销商品列表
   */
  loadDistributionGoodsList: function() {
    let that = this;
    let member_info = that.data.member_info;
    let uid = app.checkEmpty(member_info.user_info.uid, 0);

    app.sendRequest({
      url: 'System.Distribution.distributionGoodsList',
      data: {
        page: 1,
        page_size: 4,
        uid: uid,
        type: 'selected'
      },
      success: function (res) {
        if (res.code == 0) {
          let goods_list = app.checkEmpty(res.data, {});
          goods_list = app.checkEmpty(goods_list.data, []);

          for (let index in goods_list) {
            goods_list[index].pic_cover_small = app.IMG(goods_list[index].pic_cover_small);
          }

          that.setData({
            goods_list: goods_list
          })
        }
      }
    });
  }
})